﻿Option Strict On

Namespace Translation

  ''' <summary>
  ''' Represents a configuration object that is composed of a list of language and their own extensions.
  ''' </summary>
  ''' <remarks></remarks>
  Public Class LanguageConfiguration

    ''' <summary>
    ''' Stores for culture names and a list of their own extension names.
    ''' </summary>
    ''' <remarks></remarks>
    Private _Cultures As SortedDictionary(Of String, List(Of String))

    ''' <summary>
    ''' Initializes an empty culture and extension.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
      _Cultures = New SortedDictionary(Of String, List(Of String))
    End Sub

    ''' <summary>
    ''' Adds a culture name to the current object.
    ''' </summary>
    ''' <param name="Culture">The culture name to be added.</param>
    ''' <remarks></remarks>
    Public Sub Add(ByVal Culture As String)
      Add(Culture, Nothing)
    End Sub

    ''' <summary>
    ''' Adds a culture name and its extension array to the current object.
    ''' </summary>
    ''' <param name="Culture">The culture name to be added.</param>
    ''' <param name="Extensions">The array of extension name that related to specific culture to be added.</param>
    ''' <exception cref="System.ArgumentNullException">Culture is nothing or an empty string.</exception>
    ''' <remarks>This method is not allows a duplicate Culture.
    ''' One or more extension array which has a duplicate Culture will be merged with the same old Culture already exists in the current object.</remarks>
    Public Sub Add(ByVal Culture As String, ByVal Extensions() As String)
      'Checks whether Culture is nothing or an empty string.
      If String.IsNullOrEmpty(Culture) Then
        Throw New ArgumentNullException
      End If

      'Repleaces a nothing Extensions with a zero length of array.
      If Extensions Is Nothing Then
        Extensions = {}
      End If

      'Prevents duplicate Culture
      If _Cultures.ContainsKey(Culture) Then
        _Cultures.Item(Culture).AddRange(Extensions)
      Else
        _Cultures.Add(Culture, Extensions.ToList)
      End If
    End Sub

    ''' <summary>
    ''' Checks whether specified culture is already exists.
    ''' </summary>
    ''' <param name="Culture">The culture name to be determined.</param>
    ''' <returns>True if the current object contains a culture with the specified name; otherwise, false.</returns>
    ''' <remarks></remarks>
    Public Function ContainsCulture(ByVal Culture As String) As Boolean
      Return Not String.IsNullOrEmpty(Culture) AndAlso _Cultures.ContainsKey(Culture)
    End Function

    ''' <summary>
    ''' Checks whether specified extension is already exists.
    ''' </summary>
    ''' <param name="Culture">The culture name of specified extension.</param>
    ''' <param name="Extension">The extension name to be determined.</param>
    ''' <returns>True if the current object contains an extension with the specified name; otherwise, false.</returns>
    ''' <remarks></remarks>
    Public Function ContainsExtension(ByVal Culture As String, ByVal Extension As String) As Boolean
      If String.IsNullOrEmpty(Culture) OrElse String.IsNullOrEmpty(Extension) Then
        Return False
      End If

      Return _Cultures.ContainsKey(Culture) AndAlso _Cultures.Item(Culture).Contains(Extension)
    End Function

  End Class
End Namespace